package com.yuanfeng.business.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.business.dto.ShopSupplierDTO;
import com.yuanfeng.business.entity.ShopSupplierEntity;
import com.yuanfeng.business.mapper.ShopSupplierMapper;
import com.yuanfeng.business.service.ShopSupplierService;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Service
public class ShopSupplierServiceImpl extends ServiceImpl<ShopSupplierMapper, ShopSupplierEntity>
    implements ShopSupplierService{

    @Override
    public ResponseResult addShopSupplier(ShopSupplierDTO param) {
        // 获取用户信息
        UserInfoVO userInfoVO = ParseTokenUtils.getUserInfoByToken(param.getToken());
        if (userInfoVO != null) {
            param.setShopId(Integer.valueOf(userInfoVO.getShopId()));
            if (null == param.getSupplierId()) {
                this.baseMapper.insertSelective(param);
            } else {
                this.baseMapper.updateByPrimaryKeySelective(param);
            }
        } else {
            return ResponseResult.fail(BizCodeEnume.INSUFFICIENT_PERMISSIONS);
        }
        return ResponseResult.success(BizCodeEnume.OPERATION_SUCCESSFUL);
    }

    @Override
    public ResponseResult delShopSupplier(ShopSupplierDTO param) {
        // 获取用户信息
        UserInfoVO userInfoVO = ParseTokenUtils.getUserInfoByToken(param.getToken());
        if (userInfoVO != null) {
            this.baseMapper.deleteById(param.getSupplierId());
        } else {
            return ResponseResult.fail(BizCodeEnume.INSUFFICIENT_PERMISSIONS);
        }
        return ResponseResult.success(BizCodeEnume.OPERATION_SUCCESSFUL);
    }

    @Override
    public ResponseResult queryShopSupplierList(ShopSupplierDTO param) {
        List<ShopSupplierEntity> shopSuppliers = new ArrayList<ShopSupplierEntity>();
        // 获取用户信息
        UserInfoVO userInfoVO = ParseTokenUtils.getUserInfoByToken(param.getToken());
        if (userInfoVO != null) {
            shopSuppliers = this.baseMapper.selectByParam(param);
        } else {
            return ResponseResult.fail(BizCodeEnume.INSUFFICIENT_PERMISSIONS);
        }
        return ResponseResult.success(shopSuppliers);
    }
}




